using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using CMS.BLL;
using CMS.BLL.Entity;
public partial class AdminX_Gallery_AlbumDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
//        Page.Title = MultipleLanguage.OWNSITE + " - " + MultipleLanguage.GALLERIES_DETAIL.ToLower();
        if (!IsPostBack)
        {
            this.BindDataToLanguageDropDown();
            SetValidate();
            InitData();
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }

    private void SetValidate()
    {
        rfvTitle.ErrorMessage = MessagesValidate.ALBUM_EMPTY;

    }

    private void InitData()
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                {
                    tblGallery.Visible = true;
                    GetData(nItemID);
                }
            }
            else
            {
                tblGallery.Visible = false;
                imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
            }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ALBUM + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }
    private void GetData(int ItemID)
    {
        // Get data to view on UI Controls
            int nLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);  
            Albums objAlbum = new Albums();
            objAlbum.LoadById(ItemID);
            switch (nLangID)
            { 
                case 1:
                    txtTitle.Text = objAlbum.Data.AlbumName1 ;
                    break;
                case 2:
                    txtTitle.Text = objAlbum.Data.AlbumName2;
                    break;
                case 3:
                    txtTitle.Text = objAlbum.Data.AlbumName3;
                    break;
            }
            
            string sImage = objAlbum.Data.AlbumImage;
            if (sImage.Length > 0)
                imgImage.ImageUrl = Config.ROOT + Config.IMAGE_GALLERY_DEFAULT_UPLOAD  + sImage;
            else imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;
            int nPageCount = 0;
            DataTable dtbAlbum = new DataTable();
            AlbumImages objAlbumImage = new AlbumImages();
            dtbAlbum = objAlbumImage.Search(ItemID, 1, 10000, ref nPageCount);
            dtlGallery.DataSource = dtbAlbum;
            dtlGallery.DataBind();

    }

    
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }

    private void UpdateAlbumImage(int nItemID)
    {
        string sFile1 = "";
        string sFile2 = "";
        string sFile3 = "";
        string sFile4 = "";
        string sFile5 = "";
        Session[Config.SESSION_GALLERYID] = ConvertData.ConvertToString(nItemID);    
        if (iptFile1.Value != string.Empty) sFile1 = Support.UploadFile(iptFile1,"gallerydetail");
        if (iptFile2.Value != string.Empty) sFile2 = Support.UploadFile(iptFile2, "gallerydetail");
        if (iptFile3.Value != string.Empty) sFile3 = Support.UploadFile(iptFile3, "gallerydetail");
        if (iptFile4.Value != string.Empty) sFile4 = Support.UploadFile(iptFile4, "gallerydetail");
        if (iptFile5.Value != string.Empty) sFile5 = Support.UploadFile(iptFile5, "gallerydetail");

        AlbumImages objAlbumImage = new AlbumImages();
        objAlbumImage.Data.AlbumID = nItemID;
        int nResult = 0;

        if (sFile1 != "")
        {
            objAlbumImage.Data.ImageName   = sFile1;
            nResult = objAlbumImage.Insert();
        }
        if (sFile2 != "")
        {
            objAlbumImage.Data.ImageName = sFile2;
            nResult = objAlbumImage.Insert();
        }
        if (sFile3 != "")
        {
            objAlbumImage.Data.ImageName = sFile3;
            nResult = objAlbumImage.Insert();
        }
        if (sFile4 != "")
        {
            objAlbumImage.Data.ImageName = sFile4;
            nResult = objAlbumImage.Insert();
        }
        if (sFile5 != "")
        {
            objAlbumImage.Data.ImageName = sFile5;
            nResult = objAlbumImage.Insert();
        }
        GetData(nItemID);

        Session[Config.SESSION_GALLERYID] = "0";
    }

    private eAlbums AlbumsEntity()
    {
        eAlbums entityAlbum = new eAlbums();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Albums objAlbum = new Albums();
            objAlbum.LoadById(nItemID);
            entityAlbum  = objAlbum.Data;
        }
        return entityAlbum;
    }

    private eAlbums SetData()
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        int nLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);  
        eAlbums eAlbumsEntity = this.AlbumsEntity();
        Albums objAlbum = new Albums();
       
        objAlbum.Data.AlbumImage  = eAlbumsEntity.AlbumImage;
        objAlbum.Data.DateCreated = ConvertData.ConvertDatetimeToInt(DateTime.Today);   
        switch (nLangID)
        {
            case 1:
                objAlbum.Data.AlbumName1 = txtTitle.Text;
                objAlbum.Data.AlbumName2 = "";
                objAlbum.Data.AlbumName3 = "";
                break;
            case 2:
                objAlbum.Data.AlbumName2 = txtTitle.Text;
                objAlbum.Data.AlbumName1 = "";
                objAlbum.Data.AlbumName3 = "";
                break;
            case 3:
                objAlbum.Data.AlbumName3 = txtTitle.Text;
                objAlbum.Data.AlbumName2 = "";
                objAlbum.Data.AlbumName1 = "";
                break;
        }
      
        string sAlbumImage = "";
        if (iptImage.Value != "")
            sAlbumImage = Support.UploadFile(iptImage,"gallery");
        else
        {
            if (sQueery == Config.ACTION_EDIT)
                sAlbumImage = eAlbumsEntity.AlbumImage;
        }

        objAlbum.Data.AlbumImage = sAlbumImage;

        return objAlbum.Data;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nDefaultLanguage = Support.GetDefaultLanguage()-1;
        Albums objAlbum = new Albums();
        objAlbum.Data = SetData();
        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objAlbum.Update(nItemID);
            if (nActionResult > 0)
            {
                UpdateAlbumImage(nItemID);
                MessAlert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage] );
                if (objAlbum.Data.AlbumImage.ToString() != "") imgImage.ImageUrl = Config.ROOT + Config.IMAGE_GALLERY_DEFAULT_UPLOAD   + objAlbum.Data.AlbumImage.ToString();
            }

            else
                MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage ]);
        }
        else
        {
            // Is Insert
            nActionResult = objAlbum.Insert();
            if (nActionResult > 0)
            {
                UpdateAlbumImage(nActionResult);
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ALBUM + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }
          
        }   
    
    }
    protected void dtlGallery_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
         int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image imgImage = (Image)e.Item.FindControl("imgImage");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_GALLERY_DEFAULT_UPLOAD  + ConvertData.ConvertToString(nItemID) + "/" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageName"));

            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage ]);
        }
    }
    protected void dtlGallery_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        AlbumImages objAlbumImages = new AlbumImages();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objAlbumImages.DeleteById(nID);

                GetData(nItemID);
                break;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ALBUM;
        Response.Redirect(sRedirect);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitData();
    }
}
